﻿
package com.android.finaltracer.usermanage;

import android.content.Context;
import android.content.Intent;

import com.android.finaltracer.channel.ChannelProxy;
import com.android.finaltracer.engine.EngineService;
import com.android.finaltracer.engine.EventNotify;
import com.android.finaltracer.model.UserBindInfo;
import com.android.finaltracer.ui.BindCnfActivity;

public class BindPhone extends Phone {
    private final static String TAG = BindPhone.class.getSimpleName();
    public static final int PHONE_STATUS_WAIT_BIND_CNF = PHONE_STATUS_NULL + 1;// 2
    public static final int PHONE_STATUS_WAIT_BIND_RSP = PHONE_STATUS_WAIT_BIND_CNF + 1;// 3

    public BindPhone(String phoneNum, Context mContext, EventNotify mEventNotify) {
        super(phoneNum, mContext, mEventNotify);
    }

    public boolean uiBindPhoneProcess(UserBindInfo userBindInfo) {
        boolean remove = false;
        switch (userBindInfo.getCommand()) {
            case UserBindInfo.REMOTE_BIND_PHONE_REQ: {
                // Phone phone = mPm.addBindPhone(userBindInfo.getToPhoneNum());
                setPhoneStatus(PHONE_STATUS_WAIT_BIND_CNF);
                // new UserBindInfo(UserBindInfo.REMOTE_BIND_PHONE_REQ,
                // phoneNum,
                // SettingCreator.settingInit(this).getImeiStr())
                mChannelProxy.send(userBindInfo.getToPhoneNum(), userBindInfo,
                        ChannelProxy.CHANNEL_SMS);
                FtTimer.startTimer(EngineService.MODULE_BIND,
                        FtTimer.TIMER_ID_BIND_WAIT_BIND_CNF,
                        userBindInfo.getToPhoneNum());
                retryCount++;
                break;
            }
            case UserBindInfo.REMOTE_BIND_PHONE_CNF: {
                // new UserBindInfo(UserBindInfo.REMOTE_BIND_PHONE_CNF,
                // phoneNum,SettingCreator.settingInit(this).getImeiStr(), true,
                // true) //accpet&bind req
                // new UserBindInfo(UserBindInfo.REMOTE_BIND_PHONE_CNF,
                // phoneNum,null, true) //accept
                // new UserBindInfo(UserBindInfo.REMOTE_BIND_PHONE_CNF,
                // phoneNum,
                // null) //reject
                if (userBindInfo.isRequestBind() && userBindInfo.isAccept()) {
                    // Phone phone =
                    // mPm.addBindPhone(userBindInfo.getToPhoneNum());
                    setPhoneStatus(PHONE_STATUS_WAIT_BIND_RSP);
                    FtTimer.startTimer(EngineService.MODULE_BIND,
                            FtTimer.TIMER_ID_BIND_WAIT_BIND_RSP,
                            userBindInfo.getToPhoneNum());
                    retryCount++;
                } else {
                    remove = true;
                }
                mChannelProxy.send(userBindInfo.getToPhoneNum(), userBindInfo,
                        ChannelProxy.CHANNEL_SMS);
                break;
            }
            case UserBindInfo.REMOTE_BIND_PHONE_RSP: {
                // new UserBindInfo(UserBindInfo.REMOTE_BIND_PHONE_RSP,
                // phoneNum,
                // null,true) //rsp and accept
                // new UserBindInfo(UserBindInfo.REMOTE_BIND_PHONE_RSP,
                // phoneNum,
                // null) //rsp do not use this
                // new UserBindInfo(UserBindInfo.REMOTE_BIND_PHONE_RSP,
                // phoneNum,
                // null) //rsp &reject
                // Phone phone =
                // mPm.getSpecifyBindPhone(userBindInfo.getToPhoneNum());
                if (getPhoneStatus() == PHONE_STATUS_WAIT_BIND_CNF) {
                    // mPm.removeBindPhone(userBindInfo.getToPhoneNum());
                    remove = true;
                    mChannelProxy.send(userBindInfo.getToPhoneNum(), userBindInfo,
                            ChannelProxy.CHANNEL_SMS);
                }
                break;
            }
            default:
                break;
        }
        return remove;
    }

    public boolean remoteBindPhoneProcess(String from, UserBindInfo userBindInfo) {
        boolean remove = false;
        switch (userBindInfo.getCommand()) {
            case UserBindInfo.REMOTE_BIND_PHONE_REQ: {
                popUi(from, userBindInfo);
                break;
            }
            case UserBindInfo.REMOTE_BIND_PHONE_CNF: {
                // Phone phone = mPm.getSpecifyBindPhone(from);
                if (getPhoneStatus() == PHONE_STATUS_WAIT_BIND_CNF) {
                    FtTimer.stopTimer(EngineService.MODULE_BIND, from);
                    popUi(from, userBindInfo);
                    // to be added 如果接受 则要存储对方的imei
                }
                if (!userBindInfo.isRequestBind()) {
                    remove = true;
                }
                break;
            }
            case UserBindInfo.REMOTE_BIND_PHONE_RSP: {
                // Phone phone = mPm.getSpecifyBindPhone(from);
                if (getPhoneStatus() == PHONE_STATUS_WAIT_BIND_RSP) {
                    FtTimer.stopTimer(EngineService.MODULE_BIND, from);
                    // mPm.removeBindPhone(from);
                    remove = true;
                    // if(userBindInfo.isAccept()){
                    popUi(from, userBindInfo);
                    // }
                }
                break;
            }
        }
        return remove;
    }

    private void popUi(String from, UserBindInfo userBindInfo) {
        Intent intent = new Intent(mContext, BindCnfActivity.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        intent.putExtra(BindCnfActivity.FROM_WHO, from);
        intent.putExtra(BindCnfActivity.USERBINDINFO_COMMAND,
                userBindInfo.getCommand());
        intent.putExtra(BindCnfActivity.USERBINDINFO_PHONENUM,
                userBindInfo.getToPhoneNum());
        intent.putExtra(BindCnfActivity.USERBINDINFO_IMEI,
                userBindInfo.getImei());
        intent.putExtra(BindCnfActivity.USERBINDINFO_ACCEPT,
                userBindInfo.isAccept());
        intent.putExtra(BindCnfActivity.USERBINDINFO_REQ_BIND,
                userBindInfo.isRequestBind());
        mContext.startActivity(intent);
    }
}
